#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"

/* widgets:
Layer_BackMusic
Background_Music
Container_Music_On
Container_Music_Text
Text_Music_yinliang
Container_Music_Button
PopupButton_Music_OFF
PopupButton_Musci_Next
PopupButton_Musci_Last
Meter_Music_Xuanzhuan
Icon_Music_Pointer
CircleProgressBar_Music_Volume
Text_Music_VolumeNum
Icon_Music_Jingdu
Container_Music_Off
Text_Music_OFF
PopupButton_Music_ON
Icon_Music_JindutiaoOff
Button_Back_MainMenu
*/
static ITUBackground* Background_Music;
static ITUContainer* Container_Music_Off;
static ITUContainer* Container_Music_On;
static ITUMeter* Meter_Music_Xuanzhuan; 
static ITUPopupButton* PopupButton_Music_Pause;
static ITUPopupButton* PopupButton_Music_Play;
static ITUText* Text_Music_yinliang;
bool BackMusicOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_Music)//首次加载
	{
		Background_Music = ituSceneFindWidget(&theScene, "Background_Music");
		assert(Background_Music);
		Container_Music_Off = ituSceneFindWidget(&theScene, "Container_Music_Off");
		assert(Container_Music_Off);
		Container_Music_On = ituSceneFindWidget(&theScene, "Container_Music_On");
		assert(Container_Music_On);
		Meter_Music_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_Music_Xuanzhuan");
		assert(Meter_Music_Xuanzhuan);
		PopupButton_Music_Pause = ituSceneFindWidget(&theScene, "PopupButton_Music_Pause");
		assert(PopupButton_Music_Pause);
		PopupButton_Music_Play = ituSceneFindWidget(&theScene, "PopupButton_Music_Play");
		assert(PopupButton_Music_Play);
		Text_Music_yinliang = ituSceneFindWidget(&theScene, "Text_Music_yinliang");
		assert(Text_Music_yinliang);
	}
	//更具参数初始化界面
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status != 0)//打开
	{
		ituWidgetSetVisible(Container_Music_Off, false);
		ituWidgetSetVisible(Container_Music_On, true);
	}
	else//关闭
	{
		ituWidgetSetVisible(Container_Music_On, false);
		ituWidgetSetVisible(Container_Music_Off, true);
	}
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status == 0x02)//播放
	{
		ituWidgetSetVisible(PopupButton_Music_Play, false);
		ituWidgetSetVisible(PopupButton_Music_Pause, true);
	}
	else if(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status == 0x01)//暂停
	{
		ituWidgetSetVisible(PopupButton_Music_Pause, false);
		ituWidgetSetVisible(PopupButton_Music_Play, true);
	}
	ituMeterSetValue(Meter_Music_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.Volume);

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_Music_yinliang->fontIndex = 0;
		Text_Music_yinliang->boldSize = 1;
		ituTextSetFontSize(Text_Music_yinliang, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_Music_yinliang->fontIndex = 8;
		Text_Music_yinliang->boldSize = 0;
		ituTextSetFontSize(Text_Music_yinliang, 16);
	}
	Set_Rotate_Block(30);//旋转步进
    return true;
}

bool BackMusicOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool BackMusicOnTimer(ITUWidget* widget, char* param)
{
	static int MeterValue = 0, MeterValue_L = 0;
	static uint32_t SendEndTime = Rotate_SendCmd_Time, SendTime = 0;

	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备操作
	MeterValue = Meter_Music_Xuanzhuan->value;
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status != 0)//设备开
	{
		if (Action_Cmd.User_Action == Clockwise_Rotation && MeterValue < 100)//有用户操作
		{
			MeterValue += Action_Cmd.Action_Times;
			if (MeterValue > 100)MeterValue = 100;
			ituMeterSetValue(Meter_Music_Xuanzhuan, MeterValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterValue > 0)
		{
			MeterValue -= Action_Cmd.Action_Times;
			if (MeterValue < 0)MeterValue = 0;
			ituMeterSetValue(Meter_Music_Xuanzhuan, MeterValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
	}
	if (MeterValue == MeterValue_L)
	{
		if (SendEndTime <= Rotate_SendCmd_Time)
			SendEndTime++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime == Rotate_SendCmd_Time)
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
		}
	}
	else
	{
		SendEndTime = 0;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.Volume = MeterValue;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.status = 0x03;//命令设置角度
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.Volume = MeterValue;
	}
	//用MSG_GET_DEVICE_STATE_EN标记来表示当前是否在旋转动作中
	if (MessageFlag[MSG_GET_DEVICE_STATE_EN] == OFF)
	{
		if (SendTime < Rotating_SendCmd_Time)
			SendTime++;
		if (SendTime == Rotating_SendCmd_Time)//0.495s发送一次命令
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
		}
	}
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status != 0)//打开
		{
			ituWidgetSetVisible(Container_Music_Off, false);
			ituWidgetSetVisible(Container_Music_On, true);
		}
		else//关闭
		{
			ituWidgetSetVisible(Container_Music_On, false);
			ituWidgetSetVisible(Container_Music_Off, true);
		}
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status == 0x02)//播放
		{
			ituWidgetSetVisible(PopupButton_Music_Play, false);
			ituWidgetSetVisible(PopupButton_Music_Pause, true);
		}
		else if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status == 0x01)//暂停
		{
			ituWidgetSetVisible(PopupButton_Music_Pause, false);
			ituWidgetSetVisible(PopupButton_Music_Play, true);
		}
		ituMeterSetValue(Meter_Music_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.Volume);
		MeterValue = Meter_Music_Xuanzhuan->value;
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_L = MeterValue;
    return true;
}

bool PopupButton_Music_OFF_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = OFF;//状态关
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x0B;//命令关
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_Music_ON_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x02;//状态开
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x01;//命令开
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	return true;
}

bool PopupButton_Music_Next_Press(ITUWidget* widget, char* param)
{
	//下一首
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x04;
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_Music_Last_Press(ITUWidget* widget, char* param)
{
	//上一首
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x05;
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_Music_Play_Press(ITUWidget* widget, char* param)
{
	//暂停
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x02;//状态播放
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x01;
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	return true;
}

bool PopupButton_Music_Pause_Press(ITUWidget* widget, char* param)
{
	//暂停
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x01;//状态暂停
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Music_Status.status = 0x02;
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	return true;
}


